import {
  Controller,
  Get,
  Post,
    UploadedFiles,
    UploadedFile,
  Body,
  Param,
  HttpException,
  HttpStatus,
  UseGuards,
    UseInterceptors,
  Delete,
  Request,
  BadRequestException,
  NotFoundException,
  Query,
} from '@nestjs/common';
import { ArticleService } from './article.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { Express } from 'express'
import * as moment from 'moment';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiProperty,
  ApiTags,
} from '@nestjs/swagger';
import { getIdDto, CreateArticleDto } from './article.dto';
import { CreateCommentDto, getIdComDto } from './comment.dto';
import { LocalAuthGuard } from '../auth/local-auth.guard';
import { RoleGuard } from '../role/role.guard';
import { Roles } from '../role/role.decorator';
import { Role } from '../auth/role';
import {FileInterceptor,} from '@nestjs/platform-express'

@ApiTags('文章接口')
@ApiBearerAuth()
@Controller('article')
export class ArticleController {
  constructor(private readonly articleService: ArticleService) {}

  @ApiOperation({ summary: '获取列表' })
  @Get('list')
  async findAll(
    @Query('page') pageNum?: number,
    @Query('size') pageSize?: number,
    @Query('zd') zd?: string,
  ) {
    const data = await this.articleService.findAll(pageNum, pageSize, zd);

    return data;
  }


    @ApiOperation({ summary: '上传大文件' })
    @Post('bigUpload')
    @UseInterceptors(FileInterceptor('file'))
    async uploadFile(@Body() body: any, @UploadedFile() file:Express.Multer.File) {

        console.log(body,file)

     //   return this.articleService.addText(body, req.user);
    }


  @ApiOperation({ summary: '添加本章' })
  @UseGuards(RoleGuard)
  @Roles(Role.addtext)
  @UseGuards(JwtAuthGuard)
  @Post('addtext')
  async addText(@Body() body: CreateArticleDto, @Request() req) {
    return this.articleService.addText(body, req.user);
  }

  @ApiOperation({ summary: '详情' })
  @Get('details')
  async getDetail(@Query() param: getIdDto, @Request() req) {
    if (!param.id) {
      throw new NotFoundException(null, '请求参数必传！');
    }
    const data = await this.articleService.getDetail(param.id, req.user);
    if (data) {
      return data;
    } else {
      throw new NotFoundException(null, '查无此项！');
    }
  }

  @ApiOperation({ summary: '删除本章' })
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.articleService.remove(+id);
  }

  @ApiOperation({ summary: '评论发布' })
  @UseGuards(RoleGuard)
  @Roles(Role.addcomment)
  @UseGuards(JwtAuthGuard)
  @Post('comment/add')
  async commentAdd(@Body() body: CreateCommentDto, @Request() req) {
    return this.articleService.commentAdd(body, req.user);
  }

  @ApiOperation({ summary: '单个文章评论详情' })
  @Get('comment/list')
  async getCommentList(@Query() param: getIdComDto) {
    if (!param.id) {
      throw new NotFoundException(null, '请求参数必传！');
    }
    const data = await this.articleService.commentList(param.id);
    //处理返回格式
    if (data) {
      const rdata = JSON.parse(JSON.stringify(data));
      rdata.forEach((item) => {
        if (item.to_uid) {
          item.huifu = rdata.find((i) => item.to_uid === i.id);
        }
      });

      return rdata;
    } else {
      throw new NotFoundException(null, '查无此项！');
    }
  }

  @ApiOperation({ summary: '最新浏览新闻' })
  @UseGuards(JwtAuthGuard)
  @Get('lately')
  async lately(@Request() req) {
    const data = await this.articleService.lately(req.user);

    if (data) {
      return data;
    } else {
      throw new NotFoundException(null, '查无此项！');
    }
  }
}
